﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Webadmin_Modules_Partner_ListEdit : bzAdminUCBase
{
    public string _ID = "";
    public string _Act = "";
    public DataTable _dt = new DataTable();
    private bzPartner bz = new bzPartner();
    override protected void loadAction()
    {
        _Act = Request.QueryString["Act"] == null ? "" : Request.QueryString["Act"];
        _ID = Request.QueryString["ID"] == null ? "-1" : Request.QueryString["ID"];
        if (!IsPostBack)
        {
            switch (_Act)
            {
                case "Edit": this.loadEdit(); break;
                case "Add": this.resetData(); break;

            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initSiteLang(bzSiteOption.Group.Administrator, bzSiteOption.None, "Partner");
    }
    private void loadEdit()
    {

        this.Navigator = this.AdminLang["MenuSlide"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["MenuPartnerList"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["Edit"].ToString();
        this.btnUpdate.Text = this.AdminLang["Edit"].ToString();

        _dt = bz.getRecord(_ID, "-1");
        DataRow dr = _dt.Rows[0];
        this.txtLink.Text = dr["Link"].ToString();
        this.rdForward_1.Enabled = false;
        this.rdForward_0.Checked = true;
        this.rdEnabled_1.Checked = Convert.ToInt16(dr["Enabled"]) <= 0 ? false : true;
        this.rdEnabled_0.Checked = !this.rdEnabled_1.Checked;
    }
    private void resetData()
    {
        this.Navigator = this.AdminLang["MenuSlide"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["MenuPartnerList"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["AddNew"].ToString();

        this.txtLink.Text = "http://www.";

        this.rdForward_0.Checked = false;
        this.rdForward_1.Checked = !this.rdForward_0.Checked;
        this.btnUpdate.Text = this.AdminLang["AddNew"].ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        switch (_Act)
        {
            case "Edit": this.doEdit(); break;
            case "Add": this.doAdd(); break;
        }
    }

    private void doEdit()
    {
        if (this.AllowEdit)
        {
            this.editImage();
        }
        else
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }
    }
    private void doAdd()
    {
        if (this.AllowEdit)
        {
            this.insertImage();
        }
        else
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }
    }
    //-----------------------------------------------------------------   
    private void editImage()
    {
        DataTable dtTmp = bz.getRecord(_ID, "-1");
        if (dtTmp.Rows.Count > 0)
        {
            string sMapPath = Server.MapPath("~") + "/Images/Partner/";
            string sSuffix = "Partner_";
            DataRow dr = dtTmp.Rows[0];
            string sLocation = dr["FileName"].ToString();
            string strName = "";
            if (this.txtFile.HasFile)
            {
                string[] arr = this.txtFile.FileName.Split('.');
                string str = arr[arr.Length - 1].ToLower().Trim();
                if (str == "png" || str == "jpg" || str == "jpeg" || str == "bmp" || str == "gif")
                {
                    strName = sSuffix + this.txtFile.FileName;
                    while (bzFile.Exist(sMapPath + strName))
                    {
                        strName = strName.Replace(".", bzStringClass.ranNum(1) + ".");
                    }
                    this.txtFile.SaveAs(sMapPath + strName);
                    bzFile.delete(sMapPath + sLocation);
                }
                else Response.Write(bzStringClass.addStringAlert(this.AdminLang["NotSuport"]));
            }
            else strName = sLocation;
            if (bz.updateRecord(_ID,"",this.txtLink.Text,"","", strName,this.rdEnabled_0.Checked ? "0" : "1") > 0)
            {
                Response.Write(bzStringClass.addStringAlert(this.AdminLang["Success"]));
                Response.Redirect(Session["Page"].ToString());
            }
        }
    }
    private void insertImage()
    {
        string sMapPath = Server.MapPath("~") + "/Images/Partner/";
        string sSuffix = "Partner_";
        if (this.txtFile.HasFile)
        {
            string[] arr = this.txtFile.FileName.Split('.');
            string str = arr[arr.Length - 1].ToLower().Trim();
            if (str == "png" || str == "jpg" || str == "jpeg" || str == "bmp" || str == "gif")
            {
                string strName = sSuffix + this.txtFile.FileName;
                while (bzFile.Exist(sMapPath + strName))
                {
                    strName = strName.Replace(".", bzStringClass.ranNum(1) + ".");
                }
                this.txtFile.SaveAs(sMapPath + strName);
                if (bz.insertRecord("",this.txtLink.Text,"","",strName, this.rdEnabled_0.Checked ? "0" : "1") > 0)
                {
                    if (this.rdForward_0.Checked)
                        Response.Redirect(Session["Page"].ToString());
                    else this.resetData();
                }
            }
            else Response.Write(bzStringClass.addStringAlert(this.AdminLang["NotSupportImage"]));
        }
        else Response.Write(bzStringClass.addStringAlert(this.AdminLang["FileRequire"]));
    }
    //-------------------------------------------------------------------
}
